package com.ruoyi.ftrl.service.impl;

import com.ruoyi.ftrl.domain.RealTimeData;
import com.ruoyi.ftrl.mapper.RealTimeDataMapper;
import com.ruoyi.ftrl.service.IRealTimeDataService;
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.stereotype.Service;  

import java.util.List;  

@Service  
public class RealTimeDataServiceImpl implements IRealTimeDataService {  

    @Autowired  
    private RealTimeDataMapper realTimeDataMapper;  

    @Override  
    public List<RealTimeData> selectAllRealTimeData() {  
        return realTimeDataMapper.selectAllRealTimeData();  
    }  

    @Override  
    public RealTimeData selectRealTimeDataById(Long id) {  
        return realTimeDataMapper.selectRealTimeDataById(id);  
    }  

    @Override  
    public int insertRealTimeData(RealTimeData realTimeData) {
        //先检查有没有最新数据
        RealTimeData realTimeData1=realTimeDataMapper.selectRealTimeDataByIdId(realTimeData.getDataItemId());
        if (realTimeData1==null){
            return realTimeDataMapper.insertRealTimeData(realTimeData);
        }else {
            // 再将相同 dataItemId 的数据标记为历史数据
            realTimeDataMapper.updateDataToHistorical(realTimeData.getDataItemId());

            // 设置为最新数据
            realTimeData.setIsLatest(1); // 1 表示最新数据
            return realTimeDataMapper.insertRealTimeData(realTimeData);
        }
    }  

    @Override  
    public int updateRealTimeData(RealTimeData realTimeData) {  
        return realTimeDataMapper.updateRealTimeData(realTimeData);  
    }  

    @Override  
    public int deleteRealTimeDataById(Long id) {  
        return realTimeDataMapper.deleteRealTimeDataById(id);  
    }  
}